//图片上传
'use strict';
const Controller = require('egg').Controller;
const fs = require('fs');
const path = require('path')
class UploadPictureController extends Controller {
    async uploadImg() {
        //   多个文件上传
        const {
            ctx,
            app
        } = this;
        // console.log(11111111111111111)
        if (ctx.request.files) {
            const file = ctx.request.files;
            let arr = [];
            for (const item of file) {
                item.filename = Math.round(Math.random() * 100 + 1) + Date.now() + path.extname(item.filename);
                // console.log(item.filename);
                const pathName1 = 'app/public/img/' + item.filename;
                const pathName = 'public/img/' + item.filename;
                const rs = fs.createReadStream(item.filepath);
                const ws = fs.createWriteStream(pathName1);
                rs.pipe(ws);
                // fs.writeFileSync(pathName1, fs.readFileSync(item.filepath));
                // const data = await app.mysql.insert('uploadimg', {
                //     url: pathName,
                // })
                arr.push(pathName)
            }
            if(arr.length === 1){
                ctx.body = {
                    code: 0,
                    msg: '上传成功',
                    data:arr[0],
                }
            }else if(arr.length>1){
                ctx.body = {
                    code: 0,
                    msg: '上传成功',
                    data:arr,
                }
            }else{
                ctx.body = {
                    code: 4000,
                    msg: '上传失败',
                }
            }
        } else {
            ctx.body = {
                code: 4005,
                msg: "没有选择图片"
            }
        }
    }
    async updataImg(){

    }
}
module.exports = UploadPictureController;